/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.rate.gift.service.impl;

import org.springblade.common.enums.rate.GiftExchangeStatusEnum;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.modules.rate.gift.entity.Gift;
import org.springblade.modules.rate.gift.entity.GiftExchange;
import org.springblade.modules.rate.gift.service.IGiftExchangeService;
import org.springblade.modules.rate.gift.vo.GiftVO;
import org.springblade.modules.rate.gift.mapper.GiftMapper;
import org.springblade.modules.rate.gift.service.IGiftService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.rate.gift.vo.detail.GiftDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 * 礼物表 服务实现类
 *
 * @author Blade
 * @since 2021-09-20
 */
@Service
public class GiftServiceImpl extends BaseServiceImpl<GiftMapper, Gift> implements IGiftService {

	@Autowired
	@Lazy
	private IGiftExchangeService giftExchangeService;

	@Override
	public IPage<GiftVO> selectGiftPage(IPage<GiftVO> page, GiftVO gift) {
		return page.setRecords(baseMapper.selectGiftPage(page, gift));
	}

	/**
	 * 礼物详情
	 *
	 * @param gift
	 */
	@Override
	public GiftDetailVO getDetail(Gift gift) {
		Gift detail = this.getOne(Condition.getQueryWrapper(gift));
		GiftDetailVO vo = new GiftDetailVO();
		BeanUtil.copyNonNull(detail,vo);
		vo.setLeftNumber(detail.getNumber());
		vo.setDetail(detail);

		//获取兑换过的礼物
		GiftExchange exchange = new GiftExchange();
		exchange.setGiftCode(detail.getCode());
		exchange.setStatus(GiftExchangeStatusEnum.DEAL.getKey());
		Integer usedNumber = giftExchangeService.count(Condition.getQueryWrapper(exchange));
		vo.setUsedNumber(usedNumber);
		return vo;
	}
}
